<?php
/**
 * Step 1: Require the Slim Framework
 *
 * If you are not using Composer, you need to require the
 * Slim Framework and register its PSR-0 autoloader.
 *
 * If you are using Composer, you can skip this step.
 */
require 'vendor/autoload.php';

use Aws\DynamoDb\DynamoDbClient;
use Aws\Common\Enum\Region;
use Aws\DynamoDb\Enum\Type;
use Aws\DynamoDb\Enum\ComparisonOperator;

// The result will always contain ConsumedCapacityUnits
//echo $result->getPath('ConsumedCapacity/CapacityUnits') . "\n";

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim();

//GET route
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name";
});

//POST route
$app->post('/messages', function () use ($app){
	$request = \Slim\Slim::getInstance()->request();
	$message = json_decode($request->getBody());
	
	$aws = Aws\Common\Aws::factory("/home/ubuntu/config.php");
	$client = $aws->get("dynamodb");
	$time = time();
	$tableName = "usermessage";
	$emailAdress = $message->email;
	$subject = $message->subject;
	$mess = $message->message;
    $name = $message->name;

	$result = $client->putItem(array(
    		'TableName' => $tableName,
    		'Item' => $client->formatAttributes(array(
        	'sentDataTime'  => (string)$time,
        	'emailAddress'  => $emailAdress,
        	'subject'       => $subject,
        	'message'       => $mess,
        	'name'          => $name
    		)),
    		'ReturnConsumedCapacity' => 'TOTAL'
	));
 
});

//PUT route
$app->put('/person/:id', function ($id) {
    //Update Person identified by $id
});

//DELETE route
$app->delete('/person/:id', function ($id) {
    //Delete Person identified by $id
});

$app->run();

